<?php

/**
 * 数据分析
 * File Name：FxController.php
 * File Encoding：UTF-8
 * File New Time：2014-8-22 13:45:03
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class FxController extends YzwController {

//    public function init() {
//        header("Content-type: text/html; charset=utf-8");
//        exit('暂停数据分析！');
//    }
    //当天
    public function actionIndex() {
        header("Content-type: text/html; charset=utf-8");
        set_time_limit(200);
        $key = 'Guest_Fx';
        $Lock = Yii::app()->cache->get($key . '_Lock');
        if (!empty($Lock)) {
            echo '已经有任务在执行！';
            exit;
        } else {
            Yii::app()->cache->set($key . '_Lock', 1, 200);
        }
        $i = 0;
        $start_time = strtotime(date('Y-m-d 00:00:00'));
        $end_time = strtotime(date('Y-m-d 23:59:59'));
        while ($i < 2) {
            $goods_id = (int) Yii::app()->cache->get($key);
            $newGoodsId = $this->runs($start_time, $end_time, $goods_id);
            Yii::app()->cache->set($key, $newGoodsId, 3600);
            $i++;
            sleep(1);
        }
        echo '执行完毕，等待下一次执行分析！$newGoodsId：' . $newGoodsId;
        Yii::app()->cache->delete($key . '_Lock');
    }

    //昨天
    public function actionZt() {
        header("Content-type: text/html; charset=utf-8");
        set_time_limit(200);
        $key = 'Guest_Fx_Zts';
        $Lock = Yii::app()->cache->get($key . '_Lock');
        if (!empty($Lock)) {
            echo '已经有任务在执行！';
            exit;
        } else {
            Yii::app()->cache->set($key . '_Lock', 1, 200);
        }
        $i = 0;
        $start_time = strtotime(date('Y-m-d 00:00:00')) - 86400;
        $end_time = strtotime(date('Y-m-d 23:59:59')) - 86400;
        while ($i < 2) {
            $goods_id = (int) Yii::app()->cache->get($key);
            $newGoodsId = $this->runs($start_time, $end_time, $goods_id);
            Yii::app()->cache->set($key, $newGoodsId, 3600);
            $i++;
            sleep(1);
        }
        echo '执行完毕，等待下一次执行分析！$newGoodsId：' . $newGoodsId;
        Yii::app()->cache->delete($key . '_Lock');
    }

    // 大前天
    public function actionQt() {
        header("Content-type: text/html; charset=utf-8");
        exit('暂停数据分析！');
        set_time_limit(200);
        $key = 'Guest_Fx_Qt';
        $i = 0;
        $start_time = strtotime(date('Y-m-d 00:00:00')) - 172800;
        $end_time = strtotime(date('Y-m-d 23:59:59')) - 172800;
        while ($i < 10) {
            $goods_id = (int) Yii::app()->cache->get($key);
            $newGoodsId = $this->runs($start_time, $end_time, $goods_id);
            Yii::app()->cache->set($key, $newGoodsId, 3600);
            $i++;
            sleep(1);
        }
        echo '执行完毕，等待下一次执行分析！$newGoodsId：' . $newGoodsId;
    }

    //来路统计分析
    public function actionRe() {
        header("Content-type: text/html; charset=utf-8");
        GuestRefererModel::model()->sendFenxi();
        echo '来路分析完成！';
    }

    //广告分析
    public function actionAd() {
        header("Content-type: text/html; charset=utf-8");
        GuestAdModel::model()->sendFenxi();
        echo '统计完成';
    }

    //把商品在上架时间内的的展示数汇总到goods表
    public function actionView() {
        header("Content-type: text/html; charset=utf-8");

        $time = time();

        $GuestFenxiModel = GuestFenxiModel::model();
        $GoodsModel = GoodsModel::model();
        //从redis取出需要分析的数据
        $list = array();
        for ($i = 0; $i < 100; $i++) {
            if (class_exists('Redis', false)) {
                $goodsId = RedisCluster::getInstance()->pop('goodsView');
                if ($goodsId) {
                    $list[] = $goodsId;
                }
            }
        }
        //如果缓存里空了，重新写入
        if (empty($list)) {
            $data = $GuestFenxiModel->findAll($GuestFenxiModel->where(array(
                        'years' => (int) date('Y', $time),
                        'month' => (int) date('m', $time),
                        'day' => (int) date('d', $time),
            )));
            if (!empty($data)) {
                $cache = array();
                foreach ($data as $rs) {
                    $cache[] = $rs['goods_id'];
                    if (class_exists('Redis', false)) {
                        RedisCluster::getInstance()->push('goodsView', $rs['goods_id']);
                    }
                }
            }
            echo '初始化完成！';
            return true;
        }
        foreach ($list as $goods_id) {
            $info = $GoodsModel->findByPk($goods_id);
            if (!empty($info)) {
                $view = 0;
                $viewList = $GuestFenxiModel->findAll($GuestFenxiModel->where(array('goods_id' => $goods_id)));
                if (!empty($viewList)) {
                    foreach ($viewList as $rs) {
                        $view += (int) $rs['view'];
                    }
                }
                $info->view = $view;
                $info->save();
                unset($info, $viewList, $view);
            }
        }
        echo '点击数分析完成！';
    }

    //更新tag现在上架中的数量，每天执行一到两次即可
    public function actionTag() {
        header("Content-type: text/html; charset=utf-8");

        $time = time();

        $TagsContentModel = TagsContentModel::model();
        $TagsModel = TagsModel::model();
        //从redis取出需要分析的数据
        $list = array();
        for ($i = 0; $i < 100; $i++) {
            if (class_exists('Redis', false)) {
                $tagid = RedisCluster::getInstance()->pop('tagStartSum');
                if ($tagid) {
                    $list[] = $tagid;
                }
            }
        }
        //如果缓存里空了，重新写入
        if (empty($list)) {
            $criteria = $TagsContentModel->where(array(
                'start_time' => array('LT', $time),
                'end_time' => array('EGT', $time),
                'group' => 'tagid',
            ));
            $data = $TagsContentModel->findAll($criteria);
            if (!empty($data)) {
                $cache = array();
                foreach ($data as $rs) {
                    $cache[] = $rs['tagid'];
                    if (class_exists('Redis', false)) {
                        RedisCluster::getInstance()->push('tagStartSum', $rs['tagid']);
                    }
                }
            }
            echo '初始化完成！';
            return true;
        }

        foreach ($list as $tagid) {
            $info = $TagsModel->findByPk($tagid);
            if (!empty($info)) {
                $criteria = $TagsContentModel->where(array(
                    'tagid' => $tagid,
                    'start_time' => array('LT', $time),
                    'end_time' => array('EGT', $time),
                ));
                $info->start_sum = (int) $TagsContentModel->count($criteria);
                $info->save();
            }
        }
        echo '分析完成！';
    }

    //执行具体的任务
    protected function runs($start_time, $end_time, $goods_id = 0) {
        $guest = GuestDataModel::model();
//        $start_time = strtotime(date('Y-m-d 00:00:00')) - 86400;
//        $end_time = strtotime(date('Y-m-d 23:59:59')) - 86400;
        $goods = GoodsModel::model();
        $time = time();
        $where = array(
            'order' => 'goods_id asc',
            //'status' => 1,
            'start_time' => array('LT', $end_time),
            'end_time' => array('EGT', $end_time),
            'limit' => 20,
        );
        if ($goods_id) {
            $where['goods_id'] = array('GT', $goods_id);
        }
        $data = GoodsModel::model()->findAll($goods->where($where));
        if (empty($data)) {
            //return self::U('fx/Index');
            return 0;
        }
        foreach ($data as $rs) {
            //当天浏览总数
            $view = $guest->getView($rs->goods_id, $start_time, $end_time);
            //官网成交数
            $guan_turnover = (int) $guest->getTurnoverGuan($rs->goods_id, $start_time, $end_time);
            //优站成交数
            $you_turnover = (int) $guest->getTurnoverYou($rs->goods_id, $start_time, $end_time);
            //总成交数
            $turnover = $guan_turnover + $you_turnover;
            //会员访问数
            $userfanw = $guest->getUserfanw($rs->goods_id, $start_time, $end_time);
            //游客访问数
            $youkefanw = $guest->getYoukefanw($rs->goods_id, $start_time, $end_time);
            //当日成交额 成交数*每单金额
            $sales = $rs->promo_price * $turnover;
            //记录分享结果
            try {
                GuestFenxiModel::model()->saveFenxi(array(
                    'goods_id' => $rs->goods_id,
                    'product_id' => $rs->product_id,
                    'admin_id' => GuestFenxiModel::model()->getAdminId($rs->goodsbm_id),
                    'cat_id' => $rs->cat_id,
                    'start_time' => $rs->start_time,
                    'end_time' => $rs->end_time,
                    'bm_uid' => $rs->uid,
                    'add_time' => $rs->add_time,
                    'view' => $view,
                    'turnover' => $turnover,
                    'guan_turnover' => $guan_turnover,
                    'you_turnover' => $you_turnover,
                    'userfanw' => $userfanw,
                    'youkefanw' => $youkefanw,
                    'sales' => $sales,
                    'years' => (int) date('Y', $start_time),
                    'month' => (int) date('m', $start_time),
                    'day' => (int) date('d', $start_time),
                    'logtime' => $start_time,
                ));
            } catch (Exception $exc) {
                
            }
            $goods_id = $rs->goods_id;
        }
        //return self::U('fx/Index', array('goods_id' => $goods_id));
        return $goods_id;
    }

}
